// Decompiled by GOOSIN CO,LTD Copyright 2010 Pavel Kouznetsov.
// home page: http://www.Goosin.net
// Decompiler options: packimports(3) fieldsfirst ansi space 
// Source File Name:   register_draft_ok.java

package purchase.purchase;

import include.nseer_cookie.NseerId;
import include.nseer_cookie.counter;
import include.nseer_db.nseer_db_backup1;
import java.io.IOException;
import java.net.URL;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.StringTokenizer;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import javax.servlet.jsp.JspFactory;
import validata.ValidataNumber;
import validata.ValidataRecordNumber;

public class register_draft_ok extends HttpServlet
{

	public register_draft_ok()
	{
	}

	public synchronized void service(HttpServletRequest request, HttpServletResponse response)
		throws IOException, ServletException
	{
		HttpSession dbSession = request.getSession();
		JspFactory _jspxFactory = JspFactory.getDefaultFactory();
		javax.servlet.jsp.PageContext pageContext = _jspxFactory.getPageContext(this, request, response, "", true, 8192, true);
		javax.servlet.ServletContext dbApplication = dbSession.getServletContext();
		try
		{
			HttpSession session = request.getSession();
			nseer_db_backup1 purchase_db = new nseer_db_backup1(dbApplication);
			nseer_db_backup1 purchasedb = new nseer_db_backup1(dbApplication);
			nseer_db_backup1 stock_db = new nseer_db_backup1(dbApplication);
			nseer_db_backup1 purchase_db1 = new nseer_db_backup1(dbApplication);
			nseer_db_backup1 fund_db = new nseer_db_backup1(dbApplication);
			if (purchase_db.conn((String)dbSession.getAttribute("unit_db_name")) && purchasedb.conn((String)dbSession.getAttribute("unit_db_name")) && stock_db.conn((String)dbSession.getAttribute("unit_db_name")) && purchase_db1.conn((String)dbSession.getAttribute("unit_db_name")) && fund_db.conn((String)dbSession.getAttribute("unit_db_name")))
			{
				ValidataRecordNumber vrn = new ValidataRecordNumber();
				counter count = new counter(dbApplication);
				ValidataNumber validata = new ValidataNumber();
				String list_price = request.getParameter("list_price");
				String amount = request.getParameter("amount");
				String pay_ID_group = request.getParameter("pay_ID_group");
				String choice_group = request.getParameter("choice_group");
				String register = request.getParameter("register");
				String register_time = request.getParameter("register_time");
				String product_ID = request.getParameter("product_ID");
				String demand_amount1 = request.getParameter("amount");
				String product_name = request.getParameter("product_name");
				String provider_name[] = request.getParameterValues("provider_name");
				String provider_ID[] = request.getParameterValues("provider_ID");
				String contact_person[] = request.getParameterValues("contact_person1");
				String provider_tel[] = request.getParameterValues("provider_tel1");
				String demand_gather_time[] = request.getParameterValues("demand_gather_time1");
				String demand_amount[] = request.getParameterValues("demand_amount");
				String demand_price[] = request.getParameterValues("demand_price");
				double demand_amount8 = 0.0D;
				double demand_cost_price_sum8 = 0.0D;
				int n = 0;
				for (StringTokenizer tokenTO2 = new StringTokenizer(choice_group, ", "); tokenTO2.hasMoreTokens();)
				{
					String sql5 = (new StringBuilder("select * from purchase_apply where purchase_tag='1' and id='")).append(tokenTO2.nextToken()).append("'").toString();
					ResultSet rs5 = purchase_db.executeQuery(sql5);
					if (rs5.next())
						n++;
				}

				if (n == 0)
				{
					int p = 0;
					double demand_amount_sum = 0.0D;
					for (int i = 0; i < provider_ID.length; i++)
					{
						if (!demand_amount[i].equals(""))
							if (!validata.validata(demand_amount[i]))
								p++;
							else
								demand_amount_sum += Double.parseDouble(demand_amount[i]);
						if (!demand_price[i].equals(""))
						{
							StringTokenizer tokenTO1 = new StringTokenizer(demand_price[i], ",");
							String demand_price2;
							String demand_price1;
							for (demand_price2 = ""; tokenTO1.hasMoreTokens(); demand_price2 = (new StringBuilder(String.valueOf(demand_price2))).append(demand_price1).toString())
								demand_price1 = tokenTO1.nextToken();

							if (!validata.validata(demand_price2) || Double.parseDouble(demand_price2) < 0.0D)
								p++;
						}
					}

					if (p == 0)
					{
						String time = "";
						Date now = new Date();
						SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
						time = formatter.format(now);
						demand_amount_sum = 0.0D;
						double demand_cost_price_sum = 0.0D;
						int m = 0;
						int q = 0;
						for (int i = 0; i < provider_ID.length; i++)
						{
							if (!demand_amount[i].equals("") && !demand_price[i].equals("") && Double.parseDouble(demand_amount[i]) != 0.0D)
								q++;
							if (!demand_amount[i].equals("") && demand_price[i].equals(""))
								m++;
						}

						if (q != 0 && m == 0)
						{
							for (int i = 0; i < provider_ID.length; i++)
								if (!demand_amount[i].equals("") && Double.parseDouble(demand_amount[i]) != 0.0D)
									demand_amount_sum += Double.parseDouble(demand_amount[i]);

							if (demand_amount_sum == Double.parseDouble(amount))
							{
								String sql4;
								for (StringTokenizer tokenTO = new StringTokenizer(choice_group, ", "); tokenTO.hasMoreTokens(); purchase_db.executeUpdate(sql4))
									sql4 = (new StringBuilder("update purchase_apply set purchase_tag='1' where id='")).append(tokenTO.nextToken()).append("'").toString();

								String purchase_ID = NseerId.getId(getClass().getResource("").toString(), (String)dbSession.getAttribute("unit_db_name"));
								m = 1;
								demand_amount_sum = 0.0D;
								for (int i = 0; i < provider_ID.length; i++)
									if (!demand_amount[i].equals("") && Double.parseDouble(demand_amount[i]) != 0.0D)
									{
										StringTokenizer tokenTO1 = new StringTokenizer(demand_price[i], ",");
										String demand_price2;
										String demand_price1;
										for (demand_price2 = ""; tokenTO1.hasMoreTokens(); demand_price2 = (new StringBuilder(String.valueOf(demand_price2))).append(demand_price1).toString())
											demand_price1 = tokenTO1.nextToken();

										double amount8 = Double.parseDouble(demand_amount[i]);
										demand_amount_sum += amount8;
										double subtotal = amount8 * Double.parseDouble(demand_price2);
										demand_cost_price_sum += subtotal;
										String sql = (new StringBuilder("insert into purchase_details(purchase_ID,details_number,provider_ID,provider_name,demand_contact_person,demand_contact_person_tel,demand_amount,demand_price,demand_cost_price_sum,demand_gather_time) values('")).append(purchase_ID).append("','").append(m).append("','").append(provider_ID[i]).append("','").append(provider_name[i]).append("','").append(contact_person[i]).append("','").append(provider_tel[i]).append("','").append(amount8).append("','").append(demand_price2).append("','").append(subtotal).append("','").append(demand_gather_time[i]).append("')").toString();
										purchase_db.executeUpdate(sql);
										m++;
									}

								double price = demand_cost_price_sum / demand_amount_sum;
								String sql1 = (new StringBuilder("insert into purchase_purchase(purchase_ID,product_ID,product_name,register,register_time,demand_amount,demand_price,demand_cost_price_sum,pay_ID_group,apply_ID_group,check_tag) values('")).append(purchase_ID).append("','").append(product_ID).append("','").append(product_name).append("','").append(register).append("','").append(register_time).append("','").append(demand_amount_sum).append("','").append(price).append("','").append(demand_cost_price_sum).append("','").append(pay_ID_group).append("','").append(choice_group).append("','5')").toString();
								purchase_db.executeUpdate(sql1);
								String sqll = (new StringBuilder("update purchase_purchase set purchase_tag='1',invoice_tag='1',gather_tag='1',stock_gather_tag='1',pay_tag='1' where purchase_ID='")).append(purchase_ID).append("'").toString();
								purchase_db.executeUpdate(sqll);
								response.sendRedirect((new StringBuilder("purchase/purchase/register_choose.jsp?purchase_ID=")).append(purchase_ID).toString());
							} else
							{
								response.sendRedirect("purchase/purchase/register_draft_ok.jsp?finished_tag=0");
							}
						} else
						{
							response.sendRedirect("purchase/purchase/register_draft_ok.jsp?finished_tag=0");
						}
					} else
					{
						response.sendRedirect("purchase/purchase/register_draft_ok.jsp?finished_tag=1");
					}
				} else
				{
					response.sendRedirect("purchase/purchase/register_draft_ok.jsp?finished_tag=2");
				}
				purchase_db.commit();
				purchase_db.close();
			} else
			{
				response.sendRedirect("error_conn.htm");
			}
		}
		catch (Exception ex)
		{
			ex.printStackTrace();
		}
	}
}
